<!DOCTYPE html>
<html lang='en'>
<head>
  <meta charset='UTF-8'>
  <title>Title</title>
</head>
<body style="border:5px solid #333;">
  <h1>父窗口</h1>
  <iframe src="./iframePage.html" id='myframe'></iframe>
<script>
  const iFrame = document.getElementById('myframe')
  // iframe加载完毕后再发送消息，否则子页面接收不到message
  iFrame.onload = function(){
    // iframe加载完立即发送一条消息
    iFrame.contentWindow.postMessage('MessageFromIndex1','*');
  }

  //回调函数
  function receiveMessageFromIframePage (event) {
    console.log('receiveMessageFromIframePage', event)
  }

  //监听message事件
  window.addEventListener("message", receiveMessageFromIframePage, false);
</script>
</body>
</html>
